package com.jzo2o.health.service.impl;


import cn.hutool.core.util.ObjectUtil;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.expcetions.BadRequestException;
import com.jzo2o.common.utils.StringUtils;
import com.jzo2o.health.mapper.ReservationSettingMapper;
import com.jzo2o.health.model.domain.ReservationSetting;
import com.jzo2o.health.model.dto.request.FileDTO;

import com.jzo2o.health.model.dto.request.ReservationSettingResponse;
import com.jzo2o.health.model.dto.request.ReservationSettingUpsertReqDTO;


import com.jzo2o.health.model.dto.response.ReservationDateResDTO;
import com.jzo2o.health.service.IReservationSettingService;
import com.jzo2o.health.util.EasyExcelUtil;

import org.apache.poi.ss.usermodel.DataFormatter;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;


/**
 * @Description IReservationSettingServiceImpl
 * @Author liujiangyuan
 * @Date 2024-12-14-8:59
 */
@Service
public class IReservationSettingServiceImpl extends ServiceImpl<ReservationSettingMapper, ReservationSetting> implements IReservationSettingService {


    @Override
    public List<ReservationSettingResponse> getReservationSettingByMonth(String date) {
        if (StringUtils.isBlank(date)) {
            throw new BadRequestException("日期错误");
        }
        String date1 = date+"-01";
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        LocalDate parse = LocalDate.parse(date1, formatter);
        List<ReservationSetting> list = this.lambdaQuery().ge(ReservationSetting::getOrderDate, parse).
                lt(ReservationSetting::getOrderDate, parse.plusMonths(1)).list();
        List<ReservationSettingResponse> reservationSettingResDTOS=new ArrayList<>();

        for (ReservationSetting reservationSetting : list) {
            ReservationSettingResponse reservationSettingResponse = new ReservationSettingResponse();
            reservationSettingResponse.setDate(reservationSetting.getOrderDate().toString());
            reservationSettingResponse.setNumber(reservationSetting.getNumber());
            reservationSettingResponse.setReservations(reservationSetting.getReservations());
            reservationSettingResDTOS.add(reservationSettingResponse);
        }

        return reservationSettingResDTOS ;


    }
    @Autowired
    private ReservationSettingMapper reservationSettingMapper;

    @Override
    public void editNumberByDate(ReservationSettingUpsertReqDTO reservationSettingUpsertReqDTO) {
        if (ObjectUtil.isEmpty(reservationSettingUpsertReqDTO)) {
            throw new BadRequestException("日期错误");
        }
        ReservationSetting one = this.lambdaQuery().eq(ReservationSetting::getOrderDate, reservationSettingUpsertReqDTO.getOrderDate()).one();


        this.lambdaUpdate().set(ReservationSetting::getReservations,reservationSettingUpsertReqDTO.getNumber()).eq(ReservationSetting::getOrderDate, reservationSettingUpsertReqDTO.getOrderDate());


    }

    @Override
    public void upload(MultipartFile file) throws IOException {
        if (ObjectUtil.isEmpty(file)) {
            throw new BadRequestException("文件不能为空");
        }
        List<FileDTO> ts = EasyExcelUtil.syncReadModel(
                file.getInputStream(), FileDTO.class,0,1);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        for (FileDTO t : ts) {
            LocalDate parse = LocalDate.parse(t.getOrderDate(), formatter);
            ReservationSetting reservationSetting = new ReservationSetting().setOrderDate(parse).setNumber(t.getNumber());
            reservationSettingMapper.insert(reservationSetting);
        }






    }

    @Override
    public ReservationDateResDTO getReservationDateByMonth(String month) {
        if (StringUtils.isBlank(month)){
            throw new  BadRequestException("日期错误");
        }
        month=month+"-01";
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate parse = LocalDate.parse(month, formatter);

        List<ReservationSetting> list = this.lambdaQuery().gt(ReservationSetting::getOrderDate, parse)
                .lt(ReservationSetting::getOrderDate, parse.plusMonths(1)).list();
        if (ObjectUtil.isEmpty(list)){
            throw new BadRequestException("数据错误，请稍后");
        }
        ReservationDateResDTO reservationDateResDTO = new ReservationDateResDTO();
        List<String> dateList = reservationDateResDTO.getDateList();
        dateList=new ArrayList<>();
        for (ReservationSetting reservationSetting : list) {
            if (reservationSetting.getNumber()>reservationSetting.getReservations()){
                dateList.add(reservationSetting.getOrderDate().toString());
            }

        }
        reservationDateResDTO.setDateList(dateList);
        return reservationDateResDTO;
    }
}
